//
//  CZBaseTabBarController.m
//  nsj_iOS
//
//  Created by apple on 2016/12/7.
//  Copyright © 2016年 GCZ. All rights reserved.
//

#import "CZBaseTabBarController.h"
#import "CZMessionCenterViewController.h"
#import "CZUserLoginViewController.h"
#import "CZNavigationController.h"
#import "CZMineViewController.h"
#import "CZNoticeViewController.h"
#import "CZTaskCenterViewController.h"
#import "CZRankingViewController.h"
#import "CZMineViewController.h"
@interface CZBaseTabBarController () <UITabBarControllerDelegate>

@end

@implementation CZBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.tintColor = mRGBColor(0, 159, 122);
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = mRGBACOLOR(224, 235, 232, 1);
    [self.tabBar insertSubview:bgView atIndex:0];
    self.tabBar.opaque = NO;
    self.tabBar.backgroundImage = [[UIImage alloc]init];
    self.tabBar.shadowImage = [[UIImage alloc]init];
    self.delegate = self;
    [self initChildController];
    // Do any additional setup after loading the view.
}

#pragma mark- 程序第一次进入时跳出登录框
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"first_login"]) {
            [CZNsjHelper sendUUID];
        }
        [[NSUserDefaults standardUserDefaults] setObject:@"no_first_login" forKey:@"first_login"];
    });
}

- (void)initChildController {
    [self setupOneChildViewController:[[CZNavigationController alloc] initWithRootViewController:[[CZMessionCenterViewController alloc] init]] title:@"首页" image:@"home_icon" selectedImage:@"home_active_icon"];
    [self setupOneChildViewController:[[CZNavigationController alloc] initWithRootViewController:[[CZTaskCenterViewController alloc] init]] title:@"任务中心" image:@"task_icon" selectedImage:@"task_active_icon"];
    [self setupOneChildViewController:[[CZNavigationController alloc] initWithRootViewController:[[CZRankingViewController alloc] init]] title:@"排行榜" image:@"league_icon" selectedImage:@"league_active_icon"];
    
    [self setupOneChildViewController:[[CZNavigationController alloc] initWithRootViewController:[[CZNoticeViewController alloc] init]]  title:@"公告新闻" image:@"notice_icon" selectedImage:@"notice_active_icon"];
    
    [self setupOneChildViewController:[[CZNavigationController alloc] initWithRootViewController:[[CZMineViewController alloc] init]] title:@"个人中心" image:@"my_icon" selectedImage:@"my_active_icon"];
}

- (void)setupOneChildViewController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    viewController.tabBarItem.title = title;
//    [viewController.tabBarItem setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Bold" size:12.0], NSFontAttributeName, [UIColor redColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    
    // 改变未选中时tabbar的字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: mRGBColor(0, 201, 180), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
   
    // 改变选中时tabbar的字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: mRGBColor(0, 135, 109), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    
    if (image.length) {

        viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
        viewController.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
        
        viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(2, 0, -2, 0);
    }
    [self addChildViewController:viewController];
}

//改变图片颜色
- (UIImage *)imageWithColor:(UIColor *)color image:(UIImage *)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextClipToMask(context, rect, image.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSInteger index = [self.childViewControllers indexOfObject:viewController];
    
    if (self.selectedIndex != 4 && index == 4 && ![[NSUserDefaults standardUserDefaults] objectForKey:User_Token]) {
        CZNavigationController *mineNC = (CZNavigationController *)viewController;
        CZMineViewController *mineVC = mineNC.childViewControllers[0];
        [mineVC getUserData];
        CZUserLoginViewController * loginVC = [[[CZNsjHelper alloc] init] presentLoginViewControllerWithViewCotroller:self];
        @weakify(mineVC);
        loginVC.callBack = ^(){
            [weak_mineVC getUserData];
            weak_mineVC.userIconImageView.yy_imageURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:User_Logo]];
        };
    }
    
    if (self.selectedIndex != 4 && index == 4 && [[NSUserDefaults standardUserDefaults] objectForKey:User_Token]) {
        CZNavigationController *mineNC = (CZNavigationController *)viewController;
        CZMineViewController *mineVC = mineNC.childViewControllers[0];
        [mineVC getUserData];
    }
    
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
